package com.example.hjt_java_web_homework2.interfaces.controller;

import com.example.hjt_java_web_homework2.application.dto.AirConditionerDTO;
import com.example.hjt_java_web_homework2.application.dto.EnvironmentDataDTO;
import com.example.hjt_java_web_homework2.application.service.DeviceService;
import com.example.hjt_java_web_homework2.application.service.DeviceServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Li Chun Hei
 * @date 31/10/2025
 * @description
 */
@RestController
@RequestMapping("/api/devices")
@Tag(name = "设备管理", description = "空调和电表设备管理API")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    // 空调管理接口
    @PostMapping("/air-conditioners")
    @Operation(summary = "创建空调设备")
    public ResponseEntity<AirConditionerDTO> createAirConditioner(@Valid @RequestBody AirConditionerDTO dto) {
        AirConditionerDTO created = deviceService.createAirConditioner(dto);
        return ResponseEntity.ok(created);
    }

    @PutMapping("/air-conditioners/{id}")
    @Operation(summary = "更新空调设备")
    public ResponseEntity<AirConditionerDTO> updateAirConditioner(
            @PathVariable Long id, @Valid @RequestBody AirConditionerDTO dto) {
        AirConditionerDTO updated = deviceService.updateAirConditioner(id, dto);
        return ResponseEntity.ok(updated);
    }

    @GetMapping("/air-conditioners/{id}")
    @Operation(summary = "获取空调设备详情")
    public ResponseEntity<AirConditionerDTO> getAirConditioner(@PathVariable Long id) {
        AirConditionerDTO ac = deviceService.getAirConditionerById(id);
        return ResponseEntity.ok(ac);
    }

    @GetMapping("/air-conditioners")
    @Operation(summary = "获取所有空调设备")
    public ResponseEntity<List<AirConditionerDTO>> getAllAirConditioners() {
        List<AirConditionerDTO> acList = deviceService.getAllAirConditioners();
        return ResponseEntity.ok(acList);
    }

    @DeleteMapping("/air-conditioners/{id}")
    @Operation(summary = "删除空调设备")
    public ResponseEntity<Void> deleteAirConditioner(@PathVariable Long id) {
        deviceService.deleteAirConditioner(id);
        return ResponseEntity.ok().build();
    }

    // 环境数据接口
    @PostMapping("/environment-data")
    @Operation(summary = "记录环境数据")
    public ResponseEntity<EnvironmentDataDTO> recordEnvironmentData(@Valid @RequestBody EnvironmentDataDTO dto) {
        EnvironmentDataDTO recorded = deviceService.recordEnvironmentData(dto);
        return ResponseEntity.ok(recorded);
    }

    @GetMapping("/environment-data")
    @Operation(summary = "获取所有环境数据")
    public ResponseEntity<List<EnvironmentDataDTO>> getAllEnvironmentData() {
        List<EnvironmentDataDTO> dataList = deviceService.getAllEnvironmentData();
        return ResponseEntity.ok(dataList);
    }

    @GetMapping("/environment-data/location/{location}")
    @Operation(summary = "根据位置获取环境数据")
    public ResponseEntity<List<EnvironmentDataDTO>> getEnvironmentDataByLocation(@PathVariable String location) {
        List<EnvironmentDataDTO> dataList = deviceService.getEnvironmentDataByLocation(location);
        return ResponseEntity.ok(dataList);
    }
}
